A producer-consumer pattern is a common programming pattern employed in programming of multi-core computers or other multi-processor computers. The producer-consumer pattern includes one or more computational producer threads that produce values and one or more computational consumer threads that consume the values produced by the producer thread(s) and process the values. In one illustrative example producer-consumer pattern, one producer thread polls a web service and generates a new stock price value every time a stock price changes. In this illustrative example, one consumer thread consumes the new stock price values and responds to the stock price changes by re-evaluating a current portfolio.
In order to allow the producer thread and the consumer thread to execute concurrently, the producer thread preferably does not wait on the consumer thread. If the producer thread produces a value but the consumer thread is busy, the producer thread typically temporarily places the value into a queue data structure. When the consumer thread is ready to consume and process another value, the consumer thread checks the queue data structure for more work.